package com.example.edu.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.edu.dao.DependencyDao;
import com.example.edu.domain.Dependency;
import com.example.edu.domain.query.DependencyQuery;
import com.example.edu.domain.vo.DependencyVO;
import com.example.edu.domain.vo.PageVO;
import com.github.jsonzou.jmockdata.JMockData;
import com.github.jsonzou.jmockdata.TypeReference;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.when;

/**
 * @author yanzhao
 * @date 2022/2/10 15:05
 * @since 1.0.0
 */
@RunWith(SpringRunner.class)
//@ExtendWith(SpringExtension.class)
public class DependencyServiceTest1 {

    @Test
    public void test() {
        Page<Dependency> dependencyPage = new Page<>();
        dependencyPage.setSize(10);
        dependencyPage.setCurrent(1);
        dependencyPage.setTotal(1);
        final ArrayList<Dependency> records = new ArrayList<>();
        records.add(JMockData.mock(Dependency.class));
        dependencyPage.setRecords(records);

        final DependencyQuery dependencyQuery = new DependencyQuery();
        dependencyQuery.setPageNum(1);
        dependencyQuery.setPageSize(10);

        when(dependencyDao.selectPage(any(), any())).thenReturn(dependencyPage);
        PageVO<DependencyVO> page = dependencyService.getPage(dependencyQuery, new ArrayList<>());
        System.out.println(page);

        dependencyPage.setRecords(null);
        when(dependencyDao.selectPage(any(), any())).thenReturn(dependencyPage);
        page = dependencyService.getPage(dependencyQuery, new ArrayList<>());
        System.out.println(page);
    }

    @InjectMocks
    private DependencyService dependencyService;
    @Mock
    private DependencyDao dependencyDao;

    @Test
    public void testByMockData() {
        // stub（录制-重放），定义动作，当调用dependencyDao.selectPage方法，参数为任意值，返回值为dependencyPage
        final Page<Dependency> dependencyPage = JMockData.mock(new TypeReference<Page<Dependency>>() {
        });

        // dependencyPage.set()
//         dependencyPage.set()
        // dependencyPage.set()
        // dependencyPage.set()

        when(dependencyDao.selectPage(any(), any())).thenReturn(dependencyPage);
        // 被测试方法的入参
        final DependencyQuery dependencyQuery = JMockData.mock(DependencyQuery.class);
        // 调用方式
        PageVO<DependencyVO> page = dependencyService.getPage(dependencyQuery, new ArrayList<>());
        // 断言
        assertThat(page, is(notNullValue()));

        dependencyPage.setRecords(null);
        // stub（录制-重放）
        when(dependencyDao.selectPage(any(), any())).thenReturn(dependencyPage);
        // 调用方式
        page = dependencyService.getPage(dependencyQuery, new ArrayList<>());
        // 断言
        assertThat(page, is(notNullValue()));
    }


    @After
    public void destroy() {
        reset(dependencyDao);
    }

}
